const db = require('../app/database')

class UserModel {
	async getUserByOpenId(openId) {
		const statement = `
      SELECT * FROM user WHERE openid = ?;
    `
		const [result] = await db.execute(statement, [openId])
		return result
	}

  async getUserById(id) {
    const statement = `
      SELECT * FROM user WHERE id = ?;
    `
		const [result] = await db.execute(statement, [id])
		return result
  }

	async createUser(decryptedUserInfo) {
		const {
			nickName,
			gender,
			language,
			city,
			province,
			country,
			openId,
			avatarUrl,
		} = decryptedUserInfo

		const statement = `
      INSERT INTO 
        user(nick_name, gender, language, city, province, country, openid, avatar_url) 
        VALUES (?,?,?,?,?,?,?,?) ;
    `

		const [result] = await db.execute(statement, [
			nickName,
			gender,
			language,
			city,
			province,
			country,
			openId,
			avatarUrl
		])
		return result
	}
}

module.exports = new UserModel()
